package com.zijie.hosptal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.hosptal.common.Result;
import com.zijie.hosptal.pojo.po.DoctorSchedules;
import com.zijie.hosptal.pojo.vo.DoctorSchedulesVO;
import com.zijie.hosptal.service.DoctorSchedulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/doctorSchedules")
public class DoctorSchedulesController {
    private DoctorSchedulesService doctorSchedulesService;

    @Autowired
    public void setDoctorSchedulesService(DoctorSchedulesService doctorSchedulesService) {
        this.doctorSchedulesService = doctorSchedulesService;
    }



    @GetMapping
    public Result<?> getAllDoctorSchedules(Integer page, Integer limit,
                                           DoctorSchedules doctorSchedules
    ) {
        if (page == null && limit == null){
            List<DoctorSchedules> schedulesList = doctorSchedulesService.getAll();
            return Result.success(schedulesList);
        }else {

            Page<DoctorSchedulesVO> schedulePage = Page.of(page, limit);
            schedulePage = doctorSchedulesService.list(schedulePage, doctorSchedules);
            return Result.success(schedulePage);
        }

    }
    @PostMapping
    public Result<?> addDoctorSchedules(@RequestBody @Validated DoctorSchedules doctorSchedules, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            // 获取第一个错误信息
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            return Result.fail(errorMessage);
        }
        boolean save = doctorSchedulesService.save(doctorSchedules);
//        System.out.println(doctorSchedules.get());
        return save ? Result.success() : Result.fail("添加失败");
    }
    //删除
    @DeleteMapping("/{id}")
    public Result<?> deleteDoctorSchedules(@PathVariable Integer id) {
        boolean removeById = doctorSchedulesService.removeById(id);
        return removeById ? Result.success() : Result.fail("删除失败");
    }
    //修改活动信息
    @PutMapping
    public Result<?> updateDoctorSchedules(@RequestBody @Validated DoctorSchedules doctorSchedules, BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            // 获取第一个错误信息
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            return Result.fail(errorMessage);
        }
        boolean updateById = doctorSchedulesService.updateById(doctorSchedules);
        return updateById ? Result.success() : Result.fail("更新失败");
    }

    @GetMapping("/reservedCount/{id}")
    public Result<?> getReservedCount(@PathVariable Integer id) {
        Integer reservedCount = doctorSchedulesService.getReservedCount(id);
        return Result.success(reservedCount);
    }
}
